<input type="hidden" name="jobid" id="jobid" value="<?=$job->id;?>" />
<input type="hidden" name="companyid" id="companyid" value="<?=$company->id;?>" />

<h2><a href="<?=site_url('company/'.$company->company_name);?>"><?=$company->company_name;?></a> &ndash; <?=$job->job_title;?></h2>

<div class="block tabs spaces">
	
	<div class="hd">
		<ul>
			<li><a href="<?=site_url('company/'.$company->company_name.'/'.$job->job_title.'/updates');?>">Updates</a></li>
			<li><a href="<?=site_url('company/'.$company->company_name.'/'.$job->job_title.'/milestones');?>">Milestones</a></li>
			<li><a href="<?=site_url('company/'.$company->company_name.'/'.$job->job_title.'/costs');?>">Costs</a></li>
			<li><?php if ($permissions->createinvoice):?><a href="<?=site_url('company/'.$company->company_name.'/'.$job->job_title.'/billing');?>">Billing</li></a><?php endif;?>
			<li><a>Job Setup</a></li>
		</ul>
	<div class="clear"></div>
	</div>
	
	<div class="bd">

		<?php if ($permissions->showdepartments):?>
			
		<?php if ($permissions->adddepartments):?>
			
		<?=form_open('jobs/addCostCenter', array('id' => '', 'class' => 'assigndept'));?>
	
			<div class="column left">
				
				<h2>Assign a Department to this Job</h2>
	
				<fieldset>
					<p>
					<label>Department</label>
					<select class="text" name="costcenterid" id="costcenterid" <?php if (!$unusedcenters):?>style="display: none;"<?php endif;?>>
						<?php foreach($unusedcenters as $center):?>
						<option value="<?=$center->id;?>"><?=$center->centertitle;?></option>
										<?php endforeach;?>
					</select>
					<span class="info"><a href="">Create a New Department</a></span>
					</p>
				</fieldset>
	
				<fieldset>
					<p>
						<label>Start Date</label>
						<input class="text" type="text" name="startdate" class="datePicker {required:true,date:true}" value="<?=date('d/m/Y');?>" />
					</p>
					<p>
						<label>End Date</label>
						<input class="text" type="text" name="enddate" class="datePicker {required:true,date:true}" />
					</p>
				</fieldset>

				<fieldset>
					<p>
					
						<label>Allocated Hours</label>
						<input class="text" type="text" name="allocatedhours" id="allocatedhours" class="{required:true,number:true}" />
					</p>
				
					<!--><p>
						<label>Hourly Rate</label>
						<input class="text" type="text" name="hourlyrate" class="{required:true}" value="" />
					</p>-->
				</fieldset>
		
				<input type="hidden" class="hidden" name="jobid" value="<?=$job->id;?>" />
		
				<p>
					<input type="submit" class="submit" value="Save Department" /> or <a class="cancel hide" data:target="#assign-depts-form">Cancel</a>
				</p>

		<?=form_close();?>
	
			</div>
		
			<div class="column right">
				
				<h2>Assign Users to this Job</h2>

					<?=form_open('jobs/assignUser', array('class' => 'assignusers'));?>

						<!--><fieldset>
							<p><strong>Assign users to</strong></p>

							<p>
								<label><input type="radio" name="assignto" value="general" checked="checked" /> This Job</label><br />
							</p>
						</fieldset><-->

						<fieldset>
							<?php if ($users):?>
							<p><strong>Choose you weapons</strong></p>

							<?php foreach ($users as $user):?>
							<p><label><input type="checkbox" name="userid[]" value="<?=$user->id;?>" id="user-<?=$user->id;?>" /> <?=$user->firstname;?> <?=$user->lastname;?></label></p>
							<?php endforeach;?>

							<?php endif;?>
						</fieldset>

				<p><input type="submit" class="submit" name="submit" value="Assign" /> or <a class="cancel hide" data:target="#assign-users-form">Cancel</a></p>
				<input type="hidden" name="jobid" value="<?=$job->id;?>" />

				<?=form_close();?>
			</div>
			
			<div class="clear"/>
	
	<?php endif;?>
	

	<?php if ($costcenters):?>

	<h2>Assigned Departments</h2>
	
		<table>
		
			<thead>
				<td>Department</td>
				<td>Starts</td>
				<td>Ends</td>
				<td>Hours</td>
				<td>Logged</td>
				<td>Rate</td>
				<td>&nbsp;</td>
			</thead>
		
			<tbody>
				<?php $totalcost = 0; foreach ($costcenters as $key => $center):?>
				<tr>
					<td><?=$center->centertitle;?></td>
					<td><?php if ($center->startdate):?><?= date("d/m/Y", strtotime($center->startdate)); ?><?php endif;?></td>
					<td><?php if ($center->enddate):?><?= date("d/m/Y", strtotime($center->enddate)); ?><?php endif;?></td>
					<td><?php if ($permissions->editdepartments):?><?= floor($center->timespent);?>hrs <?=intval( ($center->timespent - floor($center->timespent)) * 60);?>mins<!-- logged <?php if($center->allocatedhours > 0):?> <?php $usedofallocated =  floor(($center->timespent / $center->allocatedhours) * 100);?> <?php if ($usedofallocated > 100) $class = 'over'; elseif ($usedofallocated > 80) $class = 'nearly'; else $class = 'under';?><span class="<?=$class;?>"> (<?=$usedofallocated;?>% of allocated)</span> <?php endif;?> <?php else:?><span style="display: block; float: right;">&pound;<?php $totalcost += ($center->hourlyrate * $center->allocatedhours); echo number_format($center->hourlyrate * $center->allocatedhours, 2);?></span> Allocated Hours: <?=$center->allocatedhours;?>hrs <?php endif;?> --></td>
					<td><?=$center->allocatedhours;?></td>
					<td>&pound;<?=$center->hourlyrate;?></td>
					<td><a href="<?=site_url('jobs/deleteDepartment/'.$center->id);?>" class="">Delete</a></td>
				</tr>
				<?php endforeach;?>

				<?php if (!$permissions->editdepartments):?>
				<strong style="display: block; float: right;">Total &pound;<?=number_format($totalcost, 2);?></strong><?php endif;?>
			</tbody>
		
		</table>

	<?php endif;?>

	<?php endif;?>


	<?php if ($assigned_users):?>

		<h2>Assigned Users</h2>

		<table>
			
			<thead>
				<tr>
					<td class="name">Name</td>
					<td class="assignments">Assignments</td>
				</tr>
			</thead>

			<tbody>
			<?php $current_user = 0;?>
	
				<?php foreach ($assigned_users as $key => $user):?>
			
				<?php if ($user->id !== $current_user):?>
			
				<?php if ($key > 0):?></td></tr><?php endif;?>
				
				<tr>
					<td class="name">
						<div class="name"><?=$user->firstname;?> <?=$user->lastname;?></div>
					</td>
					<td class="assignments">
						<?php $current_user = $user->id; endif;?>
						<?php if ($user->task):?>
							
						<span>
							<?=$user->task->activity_title;?> <a href="<?=site_url('jobs/unassignUser/'.$user->id.'/'.$job->id.'/'.$user->department->id.'/'.$user->task->id);?>">Remove</a>
						</span>
						
						<?php elseif ($user->department):?>
						
						<span>
							<?=$user->department->centertitle;?> <a href="<?=site_url('jobs/unassignUser/'.$user->id.'/'.$job->id.'/'.$user->department->id);?>">Remove</a>
						</span>
						
						<?php else:?>
							
						<span class="last">
							Job <a href="<?=site_url('jobs/unassignUser/'.$user->id.'/'.$job->id);?>">Remove</a>
						</span>
						
						<?php endif;?>
						
						<?php endforeach;?>
					</td>
				</tr>
			</tbody>
			
		</table>

	<?php else:?>
	<p>No users assigned yet.</p>
	<?php endif;?>


</div></div></div></div></div></div>


<div id="sidebar" class="yui-b">
	
	<div class="block">
		
		<div class="hd">
			<h3>Job Info</h3>
		</div>
		
		<div class="bd">
	
			<p>
				<strong>Job Title</strong><br/>
				<span class="<?php if ($permissions->editjob):?>edit_textinput<?php endif;?>" id="<?=$job->id;?>|job|job_title"><?=$job->job_title;?></span>
			</p>

			<p>
				<strong>Job Number</strong><br/>
				<span class="<?php if ($permissions->editjob):?>edit_textinput<?php endif;?>" id="<?=$job->id;?>|job|job_number"><?=$job->job_number;?></span>
			</p>
			<p>
				<strong>Primary Job Contact</strong><br/>
				<?php if (strlen($contact_select)):?>
				<span class="<?php if ($permissions->editjob):?>edit_contactselect<?php endif;?>" id="<?=$job->id;?>|job|contactid">
					<?php if (strlen($job->primary_contact_firstname) || strlen($job->primary_contact_surname)):?><?=$job->primary_contact_firstname;?> <?=$job->primary_contact_surname;?><?php else:?>None assigned<?php endif;?>
				</span>
				<?php else:?>
					You haven't added any contacts for this company. Might want to <a href="<?=site_url('companies/view/'.$company->id);?>">sort that out</a>.
				<?php endif;?>
			</p>

			<p>
				<strong>Secondary Job Contact</strong><br/>
				<?php if (strlen($contact_select)):?>
				<span class="<?php if ($permissions->editjob):?>edit_contactselect<?php endif;?>" id="<?=$job->id;?>|job|contact2id">
					<?php if (strlen($job->secondary_contact_firstname) || strlen($job->secondary_contact_surname)):?><?=$job->secondary_contact_firstname;?> <?=$job->secondary_contact_surname;?><?php else:?>None assigned<?php endif;?>
				</span>
				<?php endif;?>
			</p>

			<p>
				<strong>PO Number</strong><br/>
				<span class="edit_textinput" id="<?=$job->id;?>|job|ponumber"><?=$job->ponumber;?></span>
			</p>
			<p>
				<strong>Notes</strong><br/>
				<span class="<?php if ($permissions->editjob):?>edit_textarea<?php endif;?>" id="<?=$job->id;?>|job|job_notes"><?=nl2br($job->job_notes);?></span>
			</p>
			
		</div>
	</div>


	<?php if ($permissions->changejobstatus):?>
	<div class="block">
		
		<div class="hd">
			<h3>Job Status</h3>
		</div>
		
		<div class="bd">

		<?php if($job->job_status == 'live' && !$job->cancelled):?>

			<?=form_open('jobs/changeStatus/'.$job->id);?>
				<input type="hidden" name="jobid" value="<?=$job->id;?>" />
				<input type="hidden" name="completed" value="1" />
				<input type="submit" class="linkReplace" name="submit" value="Mark Completed" />
			<?=form_close();?>

			<?=form_open('jobs/changeStatus/'.$job->id);?>
				<input type="hidden" name="jobid" value="<?=$job->id;?>" />
				<input type="hidden" name="proposed" value="1" />
				<input type="submit" class="linkReplace" name="submit" value="Mark as Proposed" />
			<?=form_close();?>
		
		<?php endif;?>
	
		<?php if($job->job_status == 'proposed' && !$job->cancelled):?>

			<?=form_open('jobs/changeStatus/'.$job->id);?>
				<input type="hidden" name="jobid" value="<?=$job->id;?>" />
				<input type="hidden" name="live" value="1" />
				<input type="submit" class="linkReplace" name="submit" value="Mark Live" />
			<?=form_close();?>
		<?php endif;?>
	
		<?php if($job->job_status == 'complete'):?>

			<?=form_open('jobs/changeStatus/'.$job->id);?>
				<input type="hidden" name="jobid" value="<?=$job->id;?>" />
				<input type="hidden" name="live" value="1" />
				<input type="submit" class="linkReplace" name="submit" value="Make Live" />
			<?=form_close();?>
		
		<?php else:?>
		
		<?php if(!$job->cancelled):?>

			<?=form_open('jobs/changeStatus/'.$job->id);?>
				<input type="hidden" name="jobid" value="<?=$job->id;?>" />
				<input type="hidden" name="cancelled" value="1" />
				<input type="submit" class="linkReplace" name="submit" value="Cancel" />
			<?=form_close();?>
		
			<?php else:?>

			<?=form_open('jobs/changeStatus/'.$job->id);?>
				<input type="hidden" name="jobid" value="<?=$job->id;?>" />
				<input type="hidden" name="reinstate" value="1" />
				<input type="submit" class="linkReplace" name="submit" value="Reinstate" />
			<?=form_close();?>
		
		<?php endif;?>
	
		<?php endif;?>
	
		<?php if( !$job->archived && ($job->job_status == 'complete') ):?>
			
			<?=form_open('jobs/changeStatus/'.$job->id);?>
				<input type="hidden" name="jobid" value="<?=$job->id;?>" />
				<input type="hidden" name="archive" value="1" />
				<input type="submit" class="linkReplace" name="submit" value="Archive" />
			<?=form_close();?>
			
		<?php elseif ($job->archived):?>

			<?=form_open('jobs/changeStatus/'.$job->id);?>
				<input type="hidden" name="jobid" value="<?=$job->id;?>" />
				<input type="hidden" name="unarchive" value="1" />
				<input type="submit" class="linkReplace" name="submit" value="Unarchive" />
			<?=form_close();?>
			
		<?php endif;?>
		
	</div>
	
	<?php endif;?>                                                                                                                                                                                 